package com.appbee.news.client.ui;

import java.util.List;
import java.util.Set;

import javax.inject.Inject;
import javax.inject.Provider;

import com.appbee.core.client.cell.ICellEditListener;
import com.appbee.core.client.design.PageStyle;
import com.appbee.core.client.design.editablecelllist.EditableCellListResources;
import com.appbee.core.client.process.LoadingIndcator;
import com.appbee.core.client.templates.CommonTemplates;
import com.appbee.news.client.activty.IPresenter;
import com.appbee.news.client.activty.NewsActivty;
import com.appbee.news.client.text.NewsStrings;
import com.appbee.news.shared.NewsProxy;
import com.google.gwt.cell.client.Cell.Context;
import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Element;
import com.google.gwt.dom.client.NativeEvent;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.cellview.client.CellList;
import com.google.gwt.user.cellview.client.HasKeyboardSelectionPolicy.KeyboardSelectionPolicy;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.view.client.Range;
import com.google.web.bindery.requestfactory.shared.Violation;

public class NewsView extends Composite implements NewsActivty.View,ICellEditListener<NewsProxy>
{

	private static NewsViewUiBinder uiBinder = GWT
			.create(NewsViewUiBinder.class);

	interface NewsViewUiBinder extends UiBinder<Widget, NewsView>
	{
	}
	
	private final CommonTemplates html;
	
	private IPresenter presenter;

	INewsStyle newsStyle;

	@UiField(provided=true)
	PageStyle style;

	@UiField(provided=true)
	CellList<NewsProxy> listView;
	
	
	@UiField
	Button createButton;
	
	
	private NewsEditor newsEditor;
	private final Provider<NewsEditor> newsEditorProvider;

	private final EditableCellListResources listRes;

	private final LoadingIndcator loadingIndcator;

	@UiField(provided = true)
	NewsStrings newsStrings;

	@Inject
	public NewsView(INewsStyle newsStyle,
					NewsStrings newsStrings,
					PageStyle style,
					Provider<NewsEditor> newsEditorProvider,
					CommonTemplates html,
					EditableCellListResources listRes,
					LoadingIndcator loadingIndcator)
	{
		this.newsStyle = newsStyle;
		this.newsStrings = newsStrings;
		this.style = style;
		this.listRes = listRes;
		this.newsEditorProvider = newsEditorProvider;
		this.html = html;
		this.loadingIndcator = loadingIndcator;
		
	}
	
	private void init()
	{
		listView = new CellList<NewsProxy>(
new NewsCell(presenter.isEditMode(),
				html, newsStyle, listRes, this), listRes);
		listView.setLoadingIndicator(loadingIndcator);
		if (presenter.isEditMode())
			listView.addStyleName(listRes.cellListStyle().editMode());
		listView.addStyleName("testclass");
		listView.setKeyboardSelectionPolicy(KeyboardSelectionPolicy.DISABLED);
		initWidget(uiBinder.createAndBindUi(this));
		listView.setVisibleRangeAndClearData(new Range(0, 10), false);
		createButton.setStyleName(style.newButton());
		createButton.addClickHandler(new ClickHandler()
		{
			
			@Override
			public void onClick(ClickEvent event)
			{
				newsEditor.showCreateNews(createButton);
			}
		});
	
	}
	

	@Override
	public void setTotalSize(int totalSize)
	{
		
	}

	@Override
	public void showNews(int start, List<NewsProxy> list)
	{
		if(newsEditor!=null) newsEditor.close();
		listView.setRowData(list);
		
	}
	
	private NewsEditor getNewsEditor()
	{
		if(newsEditor==null)
		{
			newsEditor = newsEditorProvider.get();
		}
		return newsEditor;
	}


	@Override
	public void setPresenter(IPresenter presenter)
	{
		boolean doInit = this.presenter==null;
		this.presenter = presenter;
		if(doInit) init();
		if(presenter.isEditMode())
		{
			getNewsEditor().setPresenter(presenter);
			createButton.setVisible(true);
			
		}
		else
		{
			createButton.setVisible(false);
		}
		
	}


	@Override
	public void showViolations(Set<Violation> errors)
	{
		newsEditor.showViolations(errors);
		
	}


	@Override
	public void onEdit(Context context, Element parent, NewsProxy value,
			NativeEvent event)
	{
		newsEditor.showCreateNews(parent.getAbsoluteLeft(),parent.getAbsoluteTop());
		newsEditor.editNews(value);
		
	}


	@Override
	public void onDelete(NewsProxy value)
	{
		if (Window.confirm(newsStrings.confirmDelete()))
		{
			presenter.onDelete(value);
		}
		
	}

}
